<?php
/**
 * Created by Ling.
 * User: Ling
 * Date: 2020/9/2
 * Time:15:48
 * @email: L_ing1992@163.com
 */

namespace App\Utils\Dmdb;


use App\Utils\Dmdb\Query\DmdbProcessor;
use App\Utils\Dmdb\Schema\DmdbBuilder;
use Illuminate\Database\Connection;
use App\Utils\Dmdb\Query\DmdbGrammar as QueryGrammar;
use App\Utils\Dmdb\Schema\DmdbGrammar as SchemaGrammar;

class DmdbConnection extends Connection
{
    /**
     * Get the default query grammar instance.
     *
     * @return \Illuminate\Database\Grammar
     */
    protected function getDefaultQueryGrammar()
    {
        return $this->withTablePrefix(new QueryGrammar);
    }

    /**
     * Get a schema builder instance for the connection.
     *
     * @return \App\Utils\Dmdb\Schema\DmdbBuilder
     */
    public function getSchemaBuilder()
    {
        if (is_null($this->schemaGrammar)) {
            $this->useDefaultSchemaGrammar();
        }

        return new DmdbBuilder($this);
    }

    /**
     * Get the default schema grammar instance.
     *
     * @return \Illuminate\Database\Grammar
     */
    protected function getDefaultSchemaGrammar()
    {
        return $this->withTablePrefix(new SchemaGrammar);
    }

    /**
     * Get the default post processor instance.
     *
     * @return \App\Utils\Dmdb\Query\DmdbProcessor
     */
    protected function getDefaultPostProcessor()
    {
        return new DmdbProcessor;
    }

    protected function getDoctrineDriver()
    {
        return null;
    }

    /**
     * 处理 数据库大写，但model却想使用小写的问题
     * 小写转大写
     *
     * @param string $query
     * @param array $bindings
     * @param bool $useReadPdo
     * @return array
     */
    public function select($query, $bindings = [], $useReadPdo = true)
    {
//        dump(strtoupper($query));
        return parent::select(config('database.connections.dm.to_upper') ? strtoupper($query) : $query, $bindings, $useReadPdo);
    }

    public function update($query, $bindings = [])
    {
        return parent::update(config('database.connections.dm.to_upper') ? strtoupper($query) : $query, $bindings);
    }

    public function insert($query, $bindings = [])
    {
        return parent::insert(config('database.connections.dm.to_upper') ? strtoupper($query) : $query, $bindings);
    }

    public function delete($query, $bindings = [])
    {
        return parent::delete(config('database.connections.dm.to_upper') ? strtoupper($query) : $query, $bindings);
    }
}
